/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pvj.addresslistdemo;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.ace.ability.AceAbility;
import ohos.ace.ability.LocalParticleAbility;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

/**
 * @since 2021-11-04
 */
public class MyLocalParticleAbility implements LocalParticleAbility {
    private static MyLocalParticleAbility instance;
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, MyLocalParticleAbility.class.getName());
    Preferences preferences;
    public static final String KEY = "key_list";
    Context applicationContext;
    Context context;

    private MyLocalParticleAbility(Context context) {
        this.context = context;
        this.applicationContext = context.getApplicationContext();

        DatabaseHelper databaseHelper = new DatabaseHelper(applicationContext);
        String fileName = "main_list.xml"; // fileName表示文件名，其取值不能为空，也不能包含路径，默认存储目录可以通过context.getPreferencesDir()获取。
        preferences = databaseHelper.getPreferences(fileName);
    }




    public static MyLocalParticleAbility getInstance(Context applicationContext) {
        if (instance == null) {
            instance = new MyLocalParticleAbility(applicationContext);

        }
        return instance;
    }


    public String getContactPersonList() {
        // context入参类型为ohos.app.Context。
        String preferencesString = preferences.getString(KEY, "");
        HiLog.info(TAG, "getContactPersonList preferencesString : " + preferencesString);
        return preferencesString;
    }


    public void addContactPersonList(String content) {
        HiLog.info(TAG, "addContactPersonList content : " + content);
        preferences.putString(KEY, content);
        preferences.flushSync();
    }


    /**
     * 跳转系统拨打电话界面
     */
    public void doDial(String destinationNum) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withAction(IntentConstants.ACTION_DIAL) // 系统应用拨号盘
            .withUri(Uri.parse("tel:" + destinationNum)) // 设置号码
            .withFlags(2)
            .build();
        intent.setOperation(operation);
        // 启动Ability
        context.startAbility(intent, 10);
    }

    public void doMessage(String telephone) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withAction(IntentConstants.ACTION_SEND_SMS)
            .withUri(Uri.parse("smsto:" + telephone)) // 设置号码
            .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
            .build();
        intent.setOperation(operation);
        context.startAbility(intent, 11);
    }

}
